package com.example.fw;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;



public class AccountHelper extends WebDriverHelperBase{

	public AccountHelper(ApplicationManager manager) {
		super(manager);
		
	}

	public void signup(User user) {
		openUrl("/");
		click(By.xpath("//span[@class='bracket-link']/a[@href='signup_page.php']"));
		type(By.name("username"), user.login);
		type(By.name("email"), user.email);
		click(By.cssSelector("input.button"));
		
		WebElement errorMessage = findElement(By.cssSelector("table.width50 tbody tr td p"));
		if (errorMessage !=null){
			throw new RuntimeException(errorMessage.getText());
		}
		
		pause(5000);
		Msg msg = manager.getMailHelper().getNewMail(user.login, user.password);
		String confirmationLink = msg.getConfirmationLink();
		System.out.println("nice ");
		System.out.println(confirmationLink);
		openAbsoluteUrl(confirmationLink);
		type(By.name("password"), user.password);
		type(By.name("password_confirm"), user.password);
		click(By.cssSelector("input.button"));
		
	}

	public String loggedUser() {
		WebElement element = findElement(By.cssSelector("td.login-info-left span"));
		return element.getText();
	}

	public void login(User user) {
		openUrl("/");
		type(By.name("username"), user.login);
		type(By.name("password"), user.password);
		click(By.cssSelector("input.button"));
	}

	public void verifySignup(User user, User administrator) {
		click(By.xpath("//table[@class='width100']//td[@class='menu']/a[@href='/mantisbt-1.2.15/logout_page.php']"));
		login(administrator);
		assertThat(loggedUser(), equalTo(administrator.login));
		click(By.xpath("//table[@class='width100']//td[@class='menu']/a[@href='/mantisbt-1.2.15/manage_overview_page.php']"));
		click(By.xpath("//div[@align='center']//a[@href='/mantisbt-1.2.15/manage_user_page.php']"));
		assertThat(String.valueOf(checkUser(user)), equalTo("true")); 
		
	}
	
	public boolean checkUser(User user){
		List<WebElement> rows = findElements(By.xpath("//table[@class='width100']//tr[@class='row-1' or @class='row-2']"));
		for (WebElement row : rows) {
			List<WebElement> cells = row.findElements(By.tagName("td")); 
			String login = cells.get(0).findElement(By.tagName("a")).getText();
			System.out.println("Login - " + login);
			String email = cells.get(2).getText();
		    System.out.println("Email = " + email);
		    User userFromUI = new User()
		    	.setLogin(login)
		    	.setEmail(email); 
		   if (user.equals(userFromUI)) {
			   System.out.println("users equals");
			   return true;
			   
		   }
		}
		return false;
	}
	
	public void deleteUser(User user){
		List<WebElement> rows = findElements(By.xpath("//table[@class='width100']//tr[@class='row-1' or @class='row-2']"));
		for (WebElement row : rows) {
			List<WebElement> cells = row.findElements(By.tagName("td")); 
			String login = cells.get(0).findElement(By.tagName("a")).getText();
			System.out.println("Login - " + login);
			String email = cells.get(2).getText();
		    System.out.println("Email = " + email);
		    User userFromUI = new User()
		    	.setLogin(login)
		    	.setEmail(email); 
		   if (user.equals(userFromUI)) {
			   cells.get(0).findElement(By.tagName("a")).click();
			   click(By.xpath("//input[@value='Delete User']"));
			   pause(2000);
			   click(By.xpath("//input[@value='Delete Account']"));
			   return;   
		   }
		}
		
	}
	

}